﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace objass
{
    /// <summary>
    /// NodesInput.xaml 的交互逻辑
    /// </summary>
    public partial class NodesInput : Window
    {
        public List<string> SelectedNodes { get; private set; } = new List<string>();

        public NodesInput()
        {
            InitializeComponent();
            LoadLeftListBox();
        }

        // 初始化左侧列表框数据
        private void LoadLeftListBox()
        {
            var app = (App)Application.Current;
            var nodes = app.GetNodeNames(); // 假设从 App 获取节点名称
            foreach (var node in nodes)
            {
                LeftListBox.Items.Add(node);
            }
        }

        // 将选中的项从左侧列表框移动到右侧列表框
        private void MoveToRight_Click(object sender, RoutedEventArgs e)
        {
            if (LeftListBox.SelectedItem != null)
            {
                var selectedItem = LeftListBox.SelectedItem.ToString();
                RightListBox.Items.Add(selectedItem);
                LeftListBox.Items.Remove(selectedItem);
            }
        }

        // 将选中的项从右侧列表框移动到左侧列表框
        private void MoveToLeft_Click(object sender, RoutedEventArgs e)
        {
            if (RightListBox.SelectedItem != null)
            {
                var selectedItem = RightListBox.SelectedItem.ToString();
                LeftListBox.Items.Add(selectedItem);
                RightListBox.Items.Remove(selectedItem);
            }
        }

        // 确定按钮点击事件
        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            SelectedNodes.Clear();
            foreach (var item in RightListBox.Items)
            {
                SelectedNodes.Add(item.ToString());
            }
            this.DialogResult = true;
            this.Close();
        }

        // 取消按钮点击事件
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void NodeTextBox_TextChanged(object sender, RoutedEventArgs e)
        {
            // 处理 inputNode 的输入变化事件
            TextBox textBox = sender as TextBox;
            if (textBox != null)
            {
                string newText = textBox.Text;
                // WordSelector根据newText的值，定位到最接近的选项，并选中
                if (newText != string.Empty)
                {
                    for (int i = 0; i < LeftListBox.Items.Count; i++)
                    {
                        if (LeftListBox.Items[i].ToString().StartsWith(newText))
                        {
                            LeftListBox.SelectedIndex = i;
                            LeftListBox.ScrollIntoView(LeftListBox.Items[i]); // 确保选中的项在可视化界面里
                            break;
                        }
                    }
                }
                else
                {
                    LeftListBox.SelectedIndex = -1; // 清除选中项
                }
            }
        }

        private void AddNodesButton_Click(object sender, RoutedEventArgs e)
        {
            // 创建对话框窗口
            Window dialog = new Window
            {
                Title = "加入一批已有节点",
                Width = 300,
                Height = 150,
                WindowStartupLocation = WindowStartupLocation.CenterScreen,
                ResizeMode = ResizeMode.NoResize,
                Owner = Application.Current.MainWindow,
            };

            // 创建对话框内容
            StackPanel panel = new StackPanel { Margin = new Thickness(10) };

            Label label = new Label { Content = "请输入一组节点名字，以逗号间隔(未存入的名字将被忽略)" };
            TextBox textBox = new TextBox { Margin = new Thickness(0, 10, 0, 10) };

            StackPanel buttonPanel = new StackPanel { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Right };
            Button okButton = new Button { Content = "确定", Width = 75, Margin = new Thickness(5, 0, 0, 0) };
            Button cancelButton = new Button { Content = "取消", Width = 75, Margin = new Thickness(5, 0, 0, 0) };

            buttonPanel.Children.Add(okButton);
            buttonPanel.Children.Add(cancelButton);

            panel.Children.Add(label);
            panel.Children.Add(textBox);
            panel.Children.Add(buttonPanel);

            dialog.Content = panel;

            // 定义变量来接受输入框的值
            string inputValue = string.Empty;

            // 处理按钮点击事件
            okButton.Click += (s, args) =>
            {
                // 在这里处理输入框的值
                inputValue = textBox.Text;
                // 关闭对话框
                dialog.DialogResult = true;
                dialog.Close();
            };

            cancelButton.Click += (s, args) =>
            {
                dialog.DialogResult = false;
                dialog.Close();
            };

            // 显示对话框
            if (dialog.ShowDialog() == true)
            {
                // 处理输入的节点名称
                string[] nodeNames = inputValue.Split(new[] { ',', '，' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var nodeName in nodeNames)
                {
                    string trimmedNodeName = nodeName.Trim();
                    if (!string.IsNullOrEmpty(trimmedNodeName)
                        && LeftListBox.Items.Contains(trimmedNodeName)
                        && !RightListBox.Items.Contains(trimmedNodeName))
                    {
                        LeftListBox.Items.Remove(trimmedNodeName);
                        RightListBox.Items.Add(trimmedNodeName);
                    }
                }
            }
        }
    }
}
